﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNetServiceLib.Core;
using Spring.Scheduling.Quartz;
using System.Reflection;
using log4net;
using System.IO;

namespace DotNetServiceLib.Core
{
    /// <summary>
    /// We use reflection to fire a specific method (with and without arguments) on the instance of a class.
    /// Assembly's name and class's method must be provided through XML configuration file.
    /// Only value type arguments are supported with exception for the System.String type.
    /// (byte, short, int, float, double, bool, char)
    /// </summary>
    public class TaskMethod : QuartzJobObject, ITask
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private string name = "";
        private string target = "";
        private string arguments = "";
        private bool loggingIsEnabled = true;
        private string className = "";
        private string methodName = "";
        private List<KeyValuePair<string, string>> listOfArguments;

        protected override void ExecuteInternal(Quartz.JobExecutionContext context)
        {
            MethodActivator methodActivator = new MethodActivator();
            methodActivator.LoggingIsEnabled = loggingIsEnabled;
            object returnValue = null;
            listOfArguments = methodActivator.ProcessArguments(arguments);
            methodActivator.Fire(target, className, methodName, listOfArguments, ref returnValue);
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public string Target
        {
            get
            {
                return target;
            }
            set
            {
                target = value;
            }
        }
        public string Arguments
        {
            get
            {
                return arguments;
            }
            set
            {
                arguments = value;
            }
        }
        public bool LoggingIsEnabled
        {
            get
            {
                return loggingIsEnabled;
            }
            set
            {
                loggingIsEnabled = value;
            }
        }
        public string ClassName
        {
            get
            {
                return className;
            }
            set
            {
                className = value;
            }
        }
        public string MethodName
        {
            get
            {
                return methodName;
            }
            set
            {
                methodName = value;
            }
        }
    }
}
